<?php

namespace App\Http\Controllers\Admin;

use App\SubTwo;
use App\Video;
use Illuminate\Http\Request;
use App\Http\Controllers\Api\Controller;

class SubTwoController extends Base
{
    public function CateAdd(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input();
            $model = SubTwo::Add($map);
            return $model;
        }
    }

    public function SaveCate(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input();
            $model = SubTwo::where('id',$map['id'])->update($map);
            if($model){
                return 200;
            }else{
                return 400;
            }
        }
    }

    public function GetSubTwoCateList(Request $request){
        if ($request->isMethod('post')){
             $list = SubTwo::where('pid',0)->get();
             return $this->response->array($list);
        }
    }

    public function GetSubTwoCateLists(Request $request){
        if ($request->isMethod('post')){
            $list = SubTwo::
                offset(($request->input('page')-1)*10)
                ->limit(10)
                ->get();
            $page = SubTwo::count();
            return $this->response->array([
                'data' => $list,
                'zpage' =>$page,
            ]);
        }
    }

    public function GetSubTwoCate(Request $request){
        if ($request->isMethod('post')){
            $list = SubTwo::where('id',$request->input('id'))->first();
            return $this->response->array($list);

        }
    }


    public function SubTwoDels(Request $request)
    {
        if ($request->isMethod('post')){
            $rew = SubTwo::where('id',$request->input('id'))->delete();
            return $rew;
        }
    }


    public function GetSubTwoLevel2(Request $request)
    {
        if ($request->isMethod('post')){
            $list = SubTwo::where('pid',$request->input('id'))->get();
            return $this->response->array($list);
        }
    }


    public function VideoAdd(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input();
            $map['file'] = str_replace('\\','/',$map['file']);
            $model = Video::add($map);
            return $model;
        }
    }
    public function VideoSave(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input();
            unset($map['image']);
            $map['file'] = str_replace('\\','/',$map['file']);
            $model = Video::where('id',$map['id'])->update($map);
            if($model){
                return 200;
            }else{
                return 400;
            }
        }
    }


    public function GetVideoList(Request $request)
    {
        if ($request->isMethod('post')){
            $list = Video::
                with('level1')
                ->with('level2')
                ->with('image')
                ->offset(($request->input('page')-1)*10)
                ->limit(10)
                ->get();
            $page = Video::count();
            return $this->response->array([
                'data' => $list,
                'zpage' =>$page,
            ]);
        }

    }



    public function GetSubTwoVideo(Request $request)
    {
        if ($request->isMethod('post')){
            $list = Video::
                with('level1')
                ->with('level2')
                ->with('image')
                ->offset(($request->input('page')-1)*10)
                ->limit(10)
                ->get();
            $page = Video::count();
            return $this->response->array([
                'data' => $list,
                'zpage' =>$page,
            ]);
        }

    }


    public function VideoDels(Request $request)
    {
        if ($request->isMethod('post')){
            $rew = Video::where('id',$request->input('id'))->delete();
            return $rew;
        }
    }


    public function GetVideoInfo(Request $request)
    {
        $id  = $request->input('id');
        $list = Video::with('image')->where('id',$id)->first();
        $level2 = SubTwo::where('pid',$list['level1'])->get();
        $image[] = $list['image'];
        return $this->response->array([
            'data'=>$list,
            'level2'=>$level2,
            'image'=>$image
        ]);
    }


    public function SearchVideoByp(Request $request)
    {
        $list = Video::with('image')->with('level1')
            ->with('level2')->where('name','like','%'.$request->p.'%')->limit(10)->get();
        return $this->response->array([
            'data'=>$list
        ]);
    }
}
